parser grammar EffectParser;

//引用词法规则
options { tokenVocab = EffectLexer; }
//复杂语句
//step1 多语句组合
exprExt1            : orgExpression(exprExt1)*              #OrgExpr//多语句
                    | IF LEFT_PAREN expr RIGHT_PAREN
                        LEFT_BRACES exprExt1 RIGHT_BRACES   #If      //if条件语句
                    ;

//核心语句
orgExpression : coreAct aimObject aimJudge targetObjectArrys aimObjectArrys targetJudge limitStr SEMICOLON;

coreAct             : SELECT                          # Select
                    | UPDATE                          # Update
                    ;

aimObject           : OBJECTARRYS                     # Object    //树对象组
                    | INT                             # Object    //数值
                    | OBJECT                          # Object    //树对象
                    | ID                              # Object    //叶子对象
                    | aimObject AS ID                 # AsObject    //赋值
                    | aimObject (PIPE  aimObject)+    # PipeObject    //赋值_复杂1 '|'
                    | aimObject (PIPE  OBJECT)+       # PipeObject    //赋值_复杂2
                    | aimObject (PIPE  ID)+           # PipeObject    //赋值_复杂3
                    ;
aimJudge            : SET set_expr                    # SetObject
                    |                                 # SetObjectBlank//备选空
                    ;
targetObjectArrys   : FROM OBJECTARRYS                # From     //树对象组
                    | FROM OBJECT                     # From     //树对象
                    | FROM ID                         # From     //叶子对象
                    |                                 # FromBlank
                    ;

aimObjectArrys      : TO OBJECTARRYS                  # To       //树对象组
                    | TO OBJECT                       # To       //树对象
                    | TO ID                           # To       //叶子对象
                    |                                 # ToBlank
                    ;
targetJudge         : WHERE where_expr                # Where
                    |                                 # WhereBlank
                    ;
limitStr            : LIMIT set_expr                  # Limit
                    |                                 # LimitBlank
                    ;
//操作符语法
expr                : expr EQ expr                    # Operator_EQ       //等于
                    | expr COLON expr                 # Operator_COLON    //赋值
                    | expr NEQ expr                   # Operator_NEQ      //不等于

                    | expr LT expr                    # Operator_LT       //小于
                    | expr LTE expr                   # Operator_LTE      //小于等于
                    | expr GT expr                    # Operator_GT       //大于
                    | expr GTE expr                   # Operator_GTE      //大于等于

                    | expr ASTERISK expr              # Operator_ASTERISK //乘法
                    | expr SLASH expr                 # Operator_SLASH    //除法
                    | expr PLUS expr                  # Operator_PLUS     //加法
                    | expr MINUS expr                 # Operator_MINUS    //减法

                    | STRING                          # Operator_Expr0    //字串
                    | INT                             # Operator_Expr1    //数值
                    | OBJECT                          # Operator_Expr2    //数对象
                    | ID                              # Operator_Expr4    //叶子对象
                    | LEFT_PAREN expr RIGHT_PAREN     # Operator_PAREN    //嵌套括号 /因为存在终止符,优先级高于操作符/expr之间的嵌套
//                    | expr (COMMA expr)+              # Operator_COMMA    //嵌套逗号 * 会和前面的语法冲突
                    ;

//aimJudge_SET&limitStr 表达式连接
set_expr            : expr (COMMA expr)*              # SetExpr
                    ;
//targetJudge_WHERE 表达式连接
where_expr          : LEFT_PAREN where_expr RIGHT_PAREN                 # WhereExpr_PAREN    //嵌套括号 /where_expr之间的嵌套
                    | expr                                              # WhereExprObject
                    | where_expr (AND expr)+                            # WhereExprAnd
                    | where_expr (OR expr)+                             # WhereExprOr
                    | where_expr (AND where_expr)+                      # WhereExprAnd
                    | where_expr (OR where_expr)+                       # WhereExprOr
                    ;
